package com.ease.framework.gef4p.commands {
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4p.editparts.EdgeEditPart;
   import com.ease.framework.gef4p.editparts.NodeEditPart;
   import com.ease.framework.gef4p.editparts.PoolEditPart;
   import com.ease.framework.gef4p.model.Edge;
   import com.ease.framework.gef4p.model.Node;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.impls.Command;

   public class CreateEdgeCommand extends Command {
      private var source:Node;
      private var target:Node;
      private var nowEdge:Edge;

      private var tranEP:EdgeEditPart;
      private var sourceEP:NodeEditPart;
      private var targetEP:NodeEditPart;

      private var _type:String;

      public function CreateEdgeCommand(edge:Edge) {
         super();
         nowEdge = edge;
         source = edge.source;
         target = edge.target;
      }

      override public function execute():void {
         source.pool.addEdge(nowEdge);

         tranEP = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(nowEdge) as EdgeEditPart;
         if (tranEP == null) {
            tranEP = IEditor(Workbench.instance.currentEditor).getEditPartFactory().createEditPart(nowEdge) as EdgeEditPart;
         }
         else {
            tranEP.unremove();
         }

         var sourceEP:NodeEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(source) as NodeEditPart;
         var targetEP:NodeEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(target) as NodeEditPart;

         tranEP.setSource(sourceEP);
         tranEP.setTarget(targetEP);

         var processEditPart:PoolEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(source.pool) as PoolEditPart;

         processEditPart.addEdgeEditPart(tranEP);
      }

      override public function undo():void {
         var processEditPart:PoolEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(source.pool) as PoolEditPart;
         processEditPart.removeEdgeEditPart(tranEP);

         source.removeOutEdge(nowEdge); //  outTransitions.removeItemAt( source.outTransitions.getItemIndex( nowEdge ));
         target.removeInEdge(nowEdge); // inTransitions.removeItemAt( target.inTransitions.getItemIndex( nowEdge ));

         source.pool.removeEdge(nowEdge);
         tranEP.remove();
      }
   }
}